package com.fe.class07.shoppingTopk;

import java.util.Arrays;
import java.util.List;

/**
 * @Description Test
 * @Author sosuke :-)
 * @Date 2022/1/9 15:41
 */
public class Test {
    public static class Data {
        public int[] arr;
        public boolean[] op;

        public Data(int[] a, boolean[] o) {
            arr = a;
            op = o;
        }
    }

    public static void main(String[] args) {
        int testTimes = 100000;
        int maxValue = 10; // 用户ID
        int maxLen = 100;
        int maxK = 6;
        for (int i = 0; i < testTimes; i++) {
            Data testData = randomData(maxValue, maxLen);
            int k = (int) (Math.random() * maxK) + 1;
            int[] arr = testData.arr;
            boolean[] op = testData.op;
            List<List<Integer>> ans1 = ShoppingTopK.topK(arr, op, k);
            List<List<Integer>> ans2 = TestShoppingTopK.topK(arr, op, k);
            if (!sameAnswer(ans1, ans2)) {
                System.out.println("Fuck");
                System.out.println("k = " + k);
                System.out.println("arr = " + Arrays.toString(arr));
                System.out.println("op = " + Arrays.toString(op));
                System.out.println("ans1 = " + ans1);
                System.out.println("ans2 = " + ans2);
                for (int j = 0; j < arr.length; j++) {
                    System.out.println(arr[j] + " , " + op[j]);
                }
                return;
            }
        }
        System.out.println("Nice");
        Data testData = randomData(maxValue, 20);
        int k = 3;
        int[] arr = testData.arr;
        boolean[] op = testData.op;
        List<List<Integer>> ans = ShoppingTopK.topK(arr, op, k);
        System.out.println("k = " + k);
        System.out.println("arr = " + Arrays.toString(arr));
        System.out.println("op = " + Arrays.toString(op));
        System.out.println("ans = " + ans);
    }

    // 为了测试
    public static Data randomData(int maxValue, int maxLen) {
        // [1,maxLen]
        int len = (int) (Math.random() * maxLen) + 1;
        int[] arr = new int[len];
        boolean[] op = new boolean[len];
        for (int i = 0; i < len; i++) {
            arr[i] = (int) (Math.random() * maxValue);
            op[i] = Math.random() < 0.5;
        }
        return new Data(arr, op);
    }

    // 为了测试
    public static boolean sameAnswer(List<List<Integer>> ans1, List<List<Integer>> ans2) {
        if (ans1.size() != ans2.size()) {
            return false;
        }
        for (int i = 0; i < ans1.size(); i++) {
            List<Integer> cur1 = ans1.get(i);
            List<Integer> cur2 = ans2.get(i);
            if (cur1.size() != cur2.size()) {
                return false;
            }
            cur1.sort((a, b) -> a - b);
            cur2.sort((a, b) -> a - b);
            for (int j = 0; j < cur1.size(); j++) {
                if (!cur1.get(j).equals(cur2.get(j))) {
                    return false;
                }
            }
        }
        return true;
    }
}
